/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.timeconsistencysubsystem;

import assisisw.smartcal.entitysubsystem.Calendar;
import assisisw.smartcal.entitysubsystem.Event;
import assisisw.smartcal.entitysubsystem.User;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;

/**
 *
 * @author Francesco
 */
@Stateless ()
public class TimelineVerifier implements ITimelineVerifier 
{

    // Instance Methods
    
    /***/
    public TimelineVerifier () {}
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public Collection < Event > verify ( User user , Event event ) 
    {
        Collection < Event > res ;
        res = new ArrayList <> () ;
        for ( Calendar c : user.getOwnedCalendars () )
            for ( Event e : c.getContainedEvents () )
                if ( notOk ( event , e ) ) 
                    res.add(e);
        // should I do it also for shared calendars ?
        for ( Event e : user.getInvitedEvents() )
            if ( notOk ( event , e ) )
                res.add ( e ) ;
        return res ;
    }
    
    private boolean notOk ( Event event , Event e ) 
    {
        boolean res ;
        res = ( event.getIniDate ().compareTo ( e.getIniDate () ) < 0 && e.getIniDate().compareTo( event.getEndDate () ) < 0 )
                        || ( event.getIniDate().compareTo(e.getEndDate ())<0 &&  e.getEndDate().compareTo(event.getEndDate ()) <0 ) ;
        return res ;
    }
    
}
